#!/bin/sh

#
# chkconfig: 235 99 01
# proftpd: This shell script takes care of starting and stopping proftpd.
# It was adapted from example available on ProFTPD's home page:
# http://www.proftpd.org/docs/howto/Stopping.html
#

# ProFTPD files
FTPD_BIN=/usr/local/sbin/proftpd
FTPD_CONF=/usr/local/etc/proftpd.conf
PIDFILE=/var/run/proftpd.pid

# If PIDFILE exists, does it point to a proftpd process?

if [ -f $PIDFILE ]; then
 pid=`cat $PIDFILE`
fi

if [ ! -x $FTPD_BIN ]; then
  echo "$0: $FTPD_BIN: cannot execute"
  exit 1
fi

case $1 in

  status)
    if [ -n "$pid" ]; then
        process_name=`ps -p $pid -o comm=`
        if [[ $process_name == "proftpd" ]]; then
            echo "$0: proftpd [PID $pid] is running"
        else
            rm $PIDFILE
            echo "$0: proftpd dead, pid file existed and was removed"
        fi
    else
        echo "$0: proftpd not running"
    fi
    ;;

  start)
    if [ -n "$pid" ]; then
      echo "$0: proftpd [PID $pid] already running"
      exit
    fi

    if [ -r $FTPD_CONF ]; then
      echo "Starting proftpd..."

      $FTPD_BIN -c $FTPD_CONF
      pidof proftpd > $PIDFILE

    else
      echo "$0: cannot start proftpd -- $FTPD_CONF missing"
    fi
    ;;

  stop)
    if [ -n "$pid" ]; then
      echo "Stopping proftpd..."
      kill -TERM $pid
      rm -f $PIDFILE

    else
      echo "$0: proftpd not running"
      exit 1
    fi
    ;;

  restart)
    if [ -n "$pid" ]; then
      echo "Rehashing proftpd configuration"
      kill -HUP $pid

    else
      echo "$0: proftpd not running"
      exit 1
    fi
    ;;

  *)
    echo "usage: $0 {start|stop|restart}"
    exit 1
    ;;

esac

exit 0
